#!/bin/bash

COLOR_BLACK="\e[0;30m"
COLOR_RED="\e[0;31m"
COLOR_GREEN="\e[0;32m"
COLOR_YELLOW="\e[0;33m"
COLOR_BLUE="\e[0;34m"
COLOR_PURPLE="\e[0;35m"
COLOR_CYAN="\e[0;36m"
COLOR_WHITE="\e[0;37m"
COLOR_RES="\e[0m"

create_log_file() {
    local time_stamp=$(date "+%Y-%m-%d_%H:%M:%S")
    # Installation log file name
    LOG_FILE_NAME="install_hpckit_${time_stamp}.log"
    # Absolute path of log files
    log_file_of_abspath=${current_dir}/log/$LOG_FILE_NAME
    # Create a log file
    mkdir -p ${current_dir}/log
    touch ${log_file_of_abspath}
}

logger() {
    local level="$1"
    local msg="$2"
    local color=${COLOR_WHITE}
    local datetime=$(date "+%Y-%m-%d %H:%M:%S")
    case $level in
    INFO)
        color=${COLOR_CYAN}
        ;;
    WARN)
        color=${COLOR_YELLOW}
        ;;
    FAIL|ERROR)
        color=${COLOR_RED}
        ;;
    SUCCESS)
        color=${COLOR_GREEN}
    esac

    printf "${color}%b${COLOR_RES}\n" "${msg}"

    if [ -f "${log_file_of_abspath}" ]; then
        echo "[${datetime}] [${level}] ${msg}" >>${log_file_of_abspath}
    fi
}

logInfo () {
    logger INFO "$1"
}

logWarn () {
    logger WARN "$1"
}

logError () {
    logger ERROR "$1"
    return 1
}

logSuccess () {
    logger SUCCESS "$1"
}

logFail () {
    logger FAIL "$1"
}